/*
 *
 *  Copyright (C) 2010 GSyC/LibreSoft, Universidad Rey Juan Carlos.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author : Raúl Román López <rroman@gsyc.es>
 *
 */

package com.LibreGeoSocial.App.AR;

import java.lang.reflect.Method;
import java.util.Hashtable;

import android.app.Activity;
import android.content.Context;
import android.gesture.GestureOverlayView;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.LibreGeoSocial.App.R;

public class ARLayerManager{
	
	private Context mContext;
	private Activity activity;
	
	private RelativeLayout infoLayer;
	private RelativeLayout resourceLayer;
	private RelativeLayout extraLayer;
	
	private GestureOverlayView gesture_view;
	
	private View summary_box;
	private int num_node = -1;
	
	private View audio_player;
	
	private boolean moveSummary = false;
	
	
	public ARLayerManager(Activity activity){
		this.mContext = activity.getBaseContext();
		this.activity = activity;
	}
	
	public void setBaseLayer(View view){
		activity.setContentView(view);
	}
	
	public void addBaseLayer(View view){
		activity.addContentView(view, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
	}
	
	public void setInfoLayer(){
		infoLayer = new RelativeLayout(mContext);
		activity.addContentView(infoLayer, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
	}
	
	public void setResourceLayer(){
		resourceLayer = new RelativeLayout(mContext);
		activity.addContentView(resourceLayer, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
	}
	
	public void setExtraLayer(){
		extraLayer = new RelativeLayout(mContext);
		activity.addContentView(extraLayer, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
	}
	
	public void setMoveSummary(boolean move){
		moveSummary = move;
	}
	
	public RelativeLayout getResourceLayer(){
		return resourceLayer;
	}
	
	public int getNodeClicked(){
		return num_node;
	}
	
	public void addInfoElement(View view, LayoutParams layoutParams){
		if(layoutParams==null)
			infoLayer.addView(view, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
		else
			infoLayer.addView(view, layoutParams);
		infoLayer.invalidate();
	}
	
	public void addResourceElement(View view, LayoutParams layoutParams){
		if(layoutParams==null)
			resourceLayer.addView(view, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
		else
			resourceLayer.addView(view, layoutParams);
		resourceLayer.invalidate();
	}
	
	public void addExtraElement(View view, LayoutParams layoutParams){
		if(layoutParams==null)
			extraLayer.addView(view, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
		else
			extraLayer.addView(view, layoutParams);
		extraLayer.invalidate();
	}
	
	public boolean isChildInResourcesList(View view){
		return (resourceLayer.indexOfChild(view) != -1);
	}
	
	public boolean isChildInInfoList(View view){
		return (infoLayer.indexOfChild(view) != -1);
	}
	
	public void removeInfoElement(View view){
		if(infoLayer.indexOfChild(view) > -1)
			infoLayer.removeView(view);
	}
	
	public void removeExtraElement(View view){
		if(extraLayer.indexOfChild(view) > -1)
			extraLayer.removeView(view);
	}
	
	public void cleanInfoLayer(){
		infoLayer.removeAllViews();
		infoLayer.invalidate();
	}
	
	public void cleanResouceLayer(){
		resourceLayer.removeAllViews();
		resourceLayer.invalidate();
	}
	
	public void cleanExtraLayer(){
		extraLayer.removeAllViews();
		extraLayer.invalidate();
	}
	
	public void drawSummaryBox(int num_node, String title, String description, String distance, Bitmap image, OnClickListener clickListener){
		Log.e("ARLayerManager", Integer.toString(num_node));
		this.num_node = num_node;
		if(summary_box == null){
			LayoutInflater factory = LayoutInflater.from(mContext);
			summary_box = factory.inflate(R.layout.ar_label, null);

			extraLayer.addView(summary_box, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
		}
		
		LinearLayout ll_container = (LinearLayout)summary_box.findViewById(R.id.ar_container);
		TextView tv_title = (TextView)summary_box.findViewById(R.id.ar_title);
		TextView tv_description = (TextView)summary_box.findViewById(R.id.ar_body);
		TextView tv_distance = (TextView)summary_box.findViewById(R.id.ar_distance);
		
		if(gesture_view == null){
			gesture_view = (GestureOverlayView)summary_box.findViewById(R.id.ar_gestures);
			initGestures();
		}
		
		ll_container.setOnClickListener(clickListener);
		
		tv_title.setText(title);
		tv_description.setText(description);
		tv_distance.setText(distance);
		
		ImageView tv_image = (ImageView)summary_box.findViewById(R.id.ar_image);
		tv_image.setImageBitmap(image);
		
	}
	
	public void removeSummaryBox(){
		if(gesture_view != null)
			gesture_view.removeAllOnGesturePerformedListeners();
		gesture_view = null;
		removeExtraElement(summary_box);
		summary_box = null;
		num_node = -1;
	}
	
	public void refreshPhoto(int num_node, Bitmap image){
		if((this.num_node != num_node) || (summary_box == null))
			return;
		ImageView tv_image = (ImageView)summary_box.findViewById(R.id.ar_image);
		tv_image.setImageBitmap(image);
	}
	
	public boolean translateSummaryBox(int num_node, int left, int top, int right, int bottom){
		if(this.num_node != num_node)
			return false;
		if(!moveSummary)
			return true;
		LinearLayout ll_box = (LinearLayout)summary_box.findViewById(R.id.ar_box);
		ll_box.setPadding(left, top, right, bottom);
		return true;
	}
	
	private void initGestures(){
		try{
			// Init gestures
			ARGesturesHandler gestureHand = new ARGesturesHandler();        
			gestureHand.initGestures(mContext, gesture_view, R.raw.gestures);

			Hashtable<Object, Object> htGestures = new Hashtable<Object, Object>();


			try {

				/* Set the static methods that execute when gesture is recognize */
				Class<ARView> c = ARView.class;
				Method m = c.getDeclaredMethod("GestureNext", (Class[]) null);
				Method m2 = c.getDeclaredMethod("GesturePrevious", (Class[]) null);

				htGestures.put((Object)"Previous", m );
				htGestures.put((Object)"Next", m2 );

			} catch (NoSuchMethodException e) {			
				e.printStackTrace();
			}        

			gestureHand.setGestures(htGestures);
			/////////////////////////
		} catch (Exception e) {
			Toast.makeText(mContext, 
					e.toString(), 
					Toast.LENGTH_LONG).show();
		}
	}
	
	public void drawAudioPlayer(View view){
		if(audio_player != null)
			removeExtraElement(audio_player);
		audio_player = view;
		addExtraElement(audio_player, new LayoutParams(50,LayoutParams.FILL_PARENT));
	}
}